//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Constraint.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CConstraint::CConstraint () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phConstraint = NULL;
#endif
        _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (new QuantLib::Constraint ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::CConstraint::CConstraint (boost::shared_ptr<QuantLib::Constraint>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phConstraint = NULL;
#endif
	_ppConstraint = &childNative;
    
}
Cephei::QL::Math::Optimization::CConstraint::CConstraint (QuantLib::Constraint& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phConstraint = NULL;
#endif
	_ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (&childNative);
    
    _ConstraintOwner = owner;
    
}

Cephei::QL::Math::Optimization::CConstraint::CConstraint (CConstraint^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phConstraint = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CConstraint::CConstraint (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phConstraint = NULL;
#endif
	if (!t->IsSubclassOf(CConstraint::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CConstraint::CConstraint (QuantLib::Handle<QuantLib::Constraint>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phConstraint = &childNative;
	_ppConstraint = &static_cast<boost::shared_ptr<QuantLib::Constraint>>(childNative.currentLink());
    
    _ConstraintOwner = owner;
}
Cephei::QL::Math::Optimization::CConstraint::CConstraint (QuantLib::Handle<QuantLib::Constraint> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phConstraint = &childNative;
	_ppConstraint = &static_cast<boost::shared_ptr<QuantLib::Constraint>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CConstraint::CConstraint (QuantLib::Constraint childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phConstraint = NULL;
#endif
	_ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (new QuantLib::Constraint (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CConstraint::~CConstraint ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppConstraint != NULL)
    {
	    delete _ppConstraint;
        _ppConstraint = NULL;
    }
}
Cephei::QL::Math::Optimization::CConstraint::!CConstraint ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppConstraint != NULL)
    {
	    delete _ppConstraint;
    }
}
QuantLib::Constraint& Cephei::QL::Math::Optimization::CConstraint::GetReference ()
{
    if (_ppConstraint == NULL) throw REFNEW NativeNullException ();
	return **_ppConstraint;
}
boost::shared_ptr<QuantLib::Constraint>& Cephei::QL::Math::Optimization::CConstraint::GetShared ()
{
    if (_ppConstraint == NULL) throw REFNEW NativeNullException ();
	return *_ppConstraint;
}
QuantLib::Constraint* Cephei::QL::Math::Optimization::CConstraint::GetPointer ()
{
    if (_ppConstraint == NULL) throw REFNEW NativeNullException ();
	return &**_ppConstraint;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Constraint>& Cephei::QL::Math::Optimization::CConstraint::GetHandle ()
{
	if (_phConstraint == NULL)
	{
		_phConstraint = new Handle<QuantLib::Constraint> (*_ppConstraint);
	}
	return *_phConstraint;
}
#endif
bool Cephei::QL::Math::Optimization::CConstraint::HasNative () 
{
	return (_ppConstraint != NULL);
}

Boolean Cephei::QL::Math::Optimization::CConstraint::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppConstraint)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Optimization::CConstraint::Test (Cephei::QL::Math::IArray^ p)
{
    CArray^ _Cp;
    try
    {
        _Cp = safe_cast<CArray^> (p);
        _Cp->Lock();
        QuantLib::Array& _p = static_cast<QuantLib::Array&> (_Cp->GetReference ()); 
    	bool _rv = (bool)(*_ppConstraint)->test ( _p );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp != nullptr) _Cp->Unlock();
    }
}
Double Cephei::QL::Math::Optimization::CConstraint::Update (Cephei::QL::Math::IArray^ p, Cephei::QL::Math::IArray^ direction, Double beta)
{
    CArray^ _Cp;
    CArray^ _Cdirection;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cp = safe_cast<CArray^> (p);
        _Cp->Lock();
        QuantLib::Array& _p = static_cast<QuantLib::Array&> (_Cp->GetReference ()); 
        _Cdirection = safe_cast<CArray^> (direction);
        _Cdirection->Lock();
        QuantLib::Array& _direction = static_cast<QuantLib::Array&> (_Cdirection->GetReference ()); 
        QuantLib::Real _beta = (QuantLib::Real)ValueHelper::Convert (beta); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppConstraint)->update ( _p,  _direction,  _beta );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp != nullptr) _Cp->Unlock();
        if (_Cdirection != nullptr) _Cdirection->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::IConstraint^ Cephei::QL::Math::Optimization::CConstraint_Factory::Create ()
{
    return REFNEW CConstraint ();
}
